package longcaisuyun.longcai.com.yiliubabalongcaisuyun;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zcx.helper.http.AsyCallBack;

import longcaisuyun.longcai.com.fragment.frag_main_dingdanguanli;
import longcaisuyun.longcai.com.net.PostChengGong;


//                            _ooOoo_
//                           o8888888o
//                           88" . "88
//                           (| -_- |)
//                            O\ = /O
//                        ____/`---'\____
//                      .   ' \\| |// `.
//                       / \\||| : |||// \
//                     / _||||| -:- |||||- \
//                       | | \\\ - /// | |
//                     | \_| ''\---/'' | |
//                      \ .-\__ `-` ___/-. /
//                   ___`. .' /--.--\ `. . __
//                ."" '< `.___\_<|>_/___.' >'"".
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |
//                 \ \ `-. \_ __\ /__ _/ .-` / /
//         ======`-.____`-.___\_____/___.-`____.-'======
//                            `=---='
//
//         .............................................
//         佛祖保佑             永无BUG
//         佛曰:
//                  写字楼里写字间，写字间里程序员；
//                  程序人员写程序，又拿程序换酒钱。
//                  酒醒只在网上坐，酒醉还来网下眠；
//                  酒醉酒醒日复日，网上网下年复年。
//                  但愿老死电脑间，不愿鞠躬老板前；
//                  奔驰宝马贵者趣，公交自行程序员。
//                  别人笑我忒疯癫，我笑自己命太贱；
//                  不见满街漂亮妹，哪个归得程序员？
/************************************************************/
//订单广播抢单结果

/************************************************************/

public class DingResultActivity extends Activity {

    TextView textView70, textView71, textView72, textView73, textView75, textView74, textView76, tv_time;
    Button button5, button6;
    String orid;
    String phonenum;
    String orderid;
    private frag_main_dingdanguanli dingdanguanli;
    private Dialog dialog;
    private PostChengGong postChengGong = new PostChengGong(MyApplication.myPreferences.readUid(), orid, new AsyCallBack<PostChengGong.Info>() {

        public void onSuccess(String toast, int type, PostChengGong.Info info) throws Exception {
            super.onSuccess(toast, type, info);
            if (info.message.equals("0")) {
                Toast.makeText(DingResultActivity.this, "获取数据失败", Toast.LENGTH_SHORT).show();
                MyApplication.myviewutil.stopProgressDialog();
            } else {
                if (info.percent != null && info.percent.length() > 0 && !info.percent.equals("null")) {
                    textView70.setText("您击败了" + info.percent + "%" + "的司机");
                } else {
                    textView70.setText("");
                }

                textView72.setText(info.usetime + " " + info.week);
//                    Log.e("时间",info.time+"");
                tv_time.setText(info.time);
//                    textView73.setText(info.list.get(0).getName());
                textView74.setText(info.list.get(0).getAddress());
//                    textView75.setText(info.list.get(info.list.size() - 1).getName());
                textView76.setText(info.list.get(info.list.size() - 1).getAddress());
                phonenum = info.mobile;
                orderid = info.orderid;
                MyApplication.myviewutil.stopProgressDialog();

            }

        }

        public void onFail(String toast, int type) throws Exception {

            postChengGong.execute(DingResultActivity.this);

        }

    });

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ding_result);
        View view = getWindow().getDecorView();
        dingdanguanli = new frag_main_dingdanguanli();
        // 适配工具将视图重新排布
        MyApplication.ScaleScreenHelper.loadView((ViewGroup) view);
        orid = getIntent().getStringExtra("orderid");
        ViewInit();
        DataInit();
    }

    private void DataInit() {

        MyApplication.myviewutil.startProgressDialog(DingResultActivity.this);

        postChengGong.driverid = MyApplication.myPreferences.readUid();

        postChengGong.orderid = orid;

        postChengGong.execute(this, false);

    }

    private void ViewInit() {
        LinearLayout fanhui_lin = (LinearLayout) findViewById(R.id.fanhui_lin);
        fanhui_lin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        textView70 = (TextView) findViewById(R.id.textView70);
        textView71 = (TextView) findViewById(R.id.textView71);
        textView72 = (TextView) findViewById(R.id.textView72);
        tv_time = (TextView) findViewById(R.id.tv_time);
//        textView73 = (TextView) findViewById(R.id.textView73);
//        textView75 = (TextView) findViewById(R.id.textView75);
        textView74 = (TextView) findViewById(R.id.textView74);
        textView76 = (TextView) findViewById(R.id.textView76);
        button5 = (Button) findViewById(R.id.button5);
        button6 = (Button) findViewById(R.id.button6);
        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                diag();
            }
        });
        button6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(DingResultActivity.this, DingDanOneActivity.class);
                intent.putExtra("orderid", orderid);
                intent.putExtra("type", "1");
                startActivity(intent);
            }
        });
    }

    public void diag() {
        LayoutInflater inflater = LayoutInflater.from(DingResultActivity.this);
        LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.item_caozuo_result, null);
        MyApplication.ScaleScreenHelper.loadView((ViewGroup) layout);
        dialog = new Dialog(DingResultActivity.this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.getWindow().setBackgroundDrawable(new BitmapDrawable());
        dialog.show();
        dialog.setContentView(layout);
        TextView tv_phone = (TextView) layout.findViewById(R.id.t_phone);
        TextView tv_cancle = (TextView) layout.findViewById(R.id.t_cancle);
        tv_phone.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phonenum));
//                if (ActivityCompat.checkSelfPermission(DingResultActivity.this, android.Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
//                    // TODO: Consider calling
//                    //    ActivityCompat#requestPermissions
//                    // here to request the missing permissions, and then overriding
//                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
//                    //                                          int[] grantResults)
//                    // to handle the case where the user grants the permission. See the documentation
//                    // for ActivityCompat#requestPermissions for more details.
//                    return;
//                }
                DingResultActivity.this.startActivity(intent);
                dialog.dismiss();
            }
        });
        tv_cancle.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        dingdanguanli.shuaXin();
    }

    //onkeydown

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode== KeyEvent.ACTION_DOWN)
            dingdanguanli.shuaXin();
        return super.onKeyDown(keyCode, event);
    }
}
